/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg.iss.model;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author LONPC
 */
public class Intersection {
    private int[][] intersection=new int[2][2];
    private ObjectInputStream in;
    private ObjectOutputStream out;
    private DatagramSocket socket;
    private DatagramPacket packet;
    
    
    public Intersection(int[][] intersection, DatagramSocket socket){
        this.intersection=intersection;
        this.socket=socket;
    }
    public int[][] getIntersection(){
        return intersection;
    }
    public void setIntersection(int[][] intersection){
        this.intersection = intersection;
    }
    
  
     public void attemptCrossing(){
        try {
            /*
            __|X |__
               *  x           
            _x *  __ 
              | x|  
            */
            intersection[0][0]=1;
            intersection[0][1]=0;
            intersection[1][0]=1;
            intersection[1][1]=0;
            
            byte[] intersectionByte=intersection.toString().getBytes();
            packet=new DatagramPacket(intersectionByte, intersectionByte.length);
            socket.send(packet);
            
            
        } catch (IOException ex) {
            Logger.getLogger(Vehicle.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
     
     
    public void requisitionCrossing(){
        int[][] matrixTemp=intersection;
        String aux=null; 
       int[][] matrix=receiveMatrix();
            
        for(int i=0; i<2; i++){            
            for(int j=0; j<2; j++){
                System.out.println(matrix[i][j]);
                if(matrix[i][j]!=0 && intersection[i][j]==0){
                    matrixTemp[i][j]=matrix[i][j];
                }
                else if(matrix[i][j]!=0 && intersection[i][j]!=0){
                    System.out.println("Crossing Denied");
                    try {
                        aux="denied";
                        packet= new DatagramPacket(aux.getBytes(), aux.length());
                        socket.send(packet);                    
                        break;
                    } catch (IOException ex) {
                        Logger.getLogger(Vehicle.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
            
        }
        try {
            aux="accepted";
            packet=new DatagramPacket(aux.getBytes(), aux.length());
            socket.send(packet);
        } catch (IOException ex) {
            Logger.getLogger(Vehicle.class.getName()).log(Level.SEVERE, null, ex);
        }
        intersection=matrixTemp;
        System.out.println("Crossing Accepted");
    }
    
     //método para desalocar a matriz depois que um carro passou
    private void crossingIntersection() {
        int[][] matrix=receiveMatrix();
        
        for(int i=0; i<2; i++){            
            for(int j=0; j<2; j++){
                if(matrix[i][j]!=0 && intersection[i][j]!=0){
                    intersection[i][j]=0;
                }
            }
        
        }
        System.out.println("Matriz liberada");
    }
    
    public int[][] receiveMatrix(){
        int[][] matrix=null;
        String aux=null;
        try {
            socket.receive(packet);                
            aux = (packet.toString());
            int l=0;
            for(int i=0; i<2;i++){
                for(int j=0; i<2; i++){
                    matrix[i][j]=Integer.parseInt(aux.substring(l, l+1));
                }
            }
                
                
        } catch (IOException ex) {
            Logger.getLogger(Vehicle.class.getName()).log(Level.SEVERE, null, ex);
        }
        return matrix;
    }
    
}
